package com.pricy.me;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.pricy.me.model.Category;
import com.pricy.me.model.CategoryIcon;
import com.pricy.me.model.TempData;

public class Preferences extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
            // Get the custom preference
            /*final Preference currency = (Preference) findPreference("currency");
            final com.pricy.me.model.User user = new com.pricy.me.model.User();
            user.loadData(this);
            
            currency.setSummary(user.getCurrencyCustom());
            currency.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                    public boolean onPreferenceClick(Preference preference) {
                        	final CharSequence[] items = {"�", "$", "DH"};

            				AlertDialog.Builder builder = new AlertDialog.Builder(Preferences.this);
            				builder.setTitle("Choisir une devise");
            				builder.setItems(items, new DialogInterface.OnClickListener() {
            				    public void onClick(DialogInterface dialog, int item) {
            				         currency.setSummary(items[item]);
            				         Context c = Preferences.this;
            				         Gson json = new Gson();
            				         user.setCurrency(user.getCurrencyCustomReverse((String) items[item]));
         							 String userJson = json.toJson(user);
            				         writeFile("user.info", userJson);
            				         
            				         TempData td = new TempData();
            				         td.setType(TempData.CURRENCY);
            				         td.setData(user.getCurrencyCustomReverse((String) items[item]));
            				         td.push(c);
            				         Toast.makeText(Preferences.this, ""+td.getList(c).size(), Toast.LENGTH_SHORT).show();
            				    }
            				});
            				AlertDialog alert = builder.create();
            				alert.show();
        				
                            return true;
                    }

            });*/
            
            Preference categories = (Preference) findPreference("categories");
            categories.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                public boolean onPreferenceClick(Preference preference) {
                	AlertDialog.Builder builder = new AlertDialog.Builder(Preferences.this);
        			builder.setTitle("Découvrir");
        			final CategoryIcon c = new CategoryIcon(0);
        			final boolean[] b = c.getAllChecked(Preferences.this);
        	        builder.setMultiChoiceItems(c.getAllChar(), b, new DialogInterface.OnMultiChoiceClickListener() {
						@Override
						public void onClick(DialogInterface arg0, int item, boolean checked) {}
					});
        	        
        			builder.setPositiveButton("Valider", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int item) {
							ArrayList<Category> userCategory = new ArrayList<Category>(); 
							List<Category> values = c.getAll();
							long[] checked = ((AlertDialog) dialog).getListView().getCheckItemIds();
							for (int i = 0; i < checked.length; i++) {
								userCategory.add((Category) values.get((int) checked[i]));
							}
							Gson json = new Gson();
							String jsonFormat = json.toJson(userCategory);
							writeFile("usercategory.txt", jsonFormat);
							TempData td = new TempData();
       				         td.setType(TempData.CATEGORIE);
       				         td.setData(jsonFormat);
       				         td.push(Preferences.this);
       				      
						}
					});
        			AlertDialog alert = builder.create();
        			alert.show();
                        return true;
                }

        });
    }
    private void writeFile(String file, String text){
		FileOutputStream fos;
		try {
			fos = getApplicationContext().openFileOutput(file, Context.MODE_PRIVATE);
			fos.write(text.getBytes());
        	fos.close();
		} catch (FileNotFoundException e) {
			Toast.makeText(getApplicationContext(), "ERROR: can't open file", Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			Toast.makeText(getApplicationContext(), "ERROR: can't write file", Toast.LENGTH_SHORT).show();
		}	
	}
}